# coding=utf-8

import matplotlib.pyplot as plt
import numpy
import tensorflow as tf
from PIL import Image

img = Image.open('demo.jpg')
img_ndarray = numpy.asarray(img, dtype='float32')
print(img_ndarray.shape)
img_ndarray = img_ndarray[:, :, 0]
plt.figure()
plt.subplot(2, 2, 1)
plt.imshow(img_ndarray)

w = [[-1.0, -1.0, -1.0],
     [-1.0, 9.0, -1.0],
     [-1.0, -1.0, -1.0]]

with tf.Session() as sess:
    img_ndarray = tf.reshape(img_ndarray, [1, 853, 1280, 1])
    w = tf.reshape(w, [3, 3, 1, 1])
    img_cov = tf.nn.conv2d(img_ndarray, w, strides=[1, 4, 4, 1], padding='SAME')
    image_data = sess.run(img_cov)
    print(image_data.shape)
    plt.subplot(2, 2, 2)
    plt.imshow(image_data[0, :, :, 0])

    img_pool = tf.nn.max_pool(img_ndarray, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1],
                              padding='SAME')
    image_data = sess.run(img_pool)
    plt.subplot(2, 2, 3)
    plt.imshow(image_data[0, :, :, 0])
    plt.subplot(2, 2, 4)
    img_pool = tf.nn.max_pool(img_ndarray, ksize=[1, 16, 16, 1], strides=[1, 16, 16, 1],
                              padding='SAME')
    image_data = sess.run(img_pool)
    plt.imshow(image_data[0, :, :, 0])
    plt.show()
